package com.znsd.zl.sys.emp.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.znsd.zl.sys.emp.dao.EmpDao;
import com.znsd.zl.sys.emp.entity.EmpEntity;
import com.znsd.zl.sys.emp.service.EmpService;
import com.znsd.zl.sys.sys.controller.SysUserController;
import com.znsd.zl.sys.sys.entity.SysUserEntity;
import com.znsd.zl.sys.sys.service.SysUserService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @ClassName EmpServiceImpl
 * @Description 描述
 * @author xyx
 * @date 2020-08-20 21:10:50
 * @Version v1.0
 */
@Service("tEmpService")
public class EmpServiceImpl extends ServiceImpl<EmpDao, EmpEntity> implements EmpService {
	@Autowired
	private SysUserService sysUserService;

	@Autowired
	private SysUserController controller;

	@Override
	public boolean newSave(EmpEntity empEntity) {
		SysUserEntity userEntity=new SysUserEntity();
		userEntity.setUsername(empEntity.getCreateTime());
		userEntity.setPassword("123456789");
		userEntity.setEmail("110@163.com");
		userEntity.setMobile("110");
		userEntity.setStatus(1);
		userEntity.setSalt("1");
		controller.save(userEntity);
		SysUserEntity userEntity1 = sysUserService.queryByUserName(empEntity.getCreateTime());
		int i = new Long(userEntity.getUserId()).intValue();

		/*格式化时间*/
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		/*设置创建时间*/
		empEntity.setCreateTime(df.format(new Date()));
		/*设置修改时间*/
		empEntity.setUpdateTime(df.format(new Date()));
		/*设置默认为未删除*/
		empEntity.setIsDelete(1);
		/*获取当前登录Id*/
		empEntity.setCreateUserId(((SysUserEntity) SecurityUtils.getSubject().getPrincipal()).getUserId());
		/*绑定的用户Id*/
		empEntity.setUserId(i);
		return this.save(empEntity);
	}

	@Override
	public boolean delete(List<Integer> ids) {
		System.out.println(ids.get(0));
		boolean b=true;
		Integer id=ids.get(0);
		EmpEntity byId = getById(id);
		int userId = byId.getUserId();
		long longUserId = userId;
		SysUserEntity entity=new SysUserEntity();
		entity.setUserId(longUserId);
		entity.setStatus(0);
		sysUserService.updateById(entity);
		for (int i = 0; i < ids.size(); i++) {
			EmpEntity empEntity = new EmpEntity();
			empEntity.setEmpNo(ids.get(i));
			empEntity.setIsDelete(2);
			b= this.updateById(empEntity);
		}
		return b;
	}

	@Override
	public boolean update(EmpEntity empEntity) {
		/*格式化时间*/
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		/*设置修改时间*/
		empEntity.setUpdateTime(df.format(new Date()));
		return updateById(empEntity);
	}

	@Override
	public List<EmpEntity> pages(Page page, Wrapper<EmpEntity> queryWrapper) {
		System.out.println(page.iterator());
		return null;
	}

	@Override
	public List<EmpEntity> findByName(String empName) {
		return baseMapper.findByName(empName);
	}
}
